package kr.inani.android.view.adapter;

import java.util.List;

import kr.inani.R;
import kr.inani.android.BookDetailActivity;
import kr.inani.android.BookSearchActivity;
import kr.inani.android.search.BookItem;
import kr.inani.android.view.ProfileImageView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;

public class SearchedBooksAdapter extends ItemsAdapter<BookItem> implements OnClickListener, AdapterView.OnItemClickListener
{
	public SearchedBooksAdapter(Context context, int layout, List<BookItem> list)
	{
		super(context, layout, list);
	}
	
	@Override
	public View getView(final int position, View convertView, ViewGroup parent)
	{
		if (convertView == null) {
			convertView = inflater.inflate(layout, parent, false);
		}
		Button btn = (Button) convertView.findViewById(R.id.addMeBook);
		btn.setTag(position);
		btn.setOnClickListener(this);
		
		setImageFromUrl(position, convertView);
		setTitle(position, (TextView) convertView.findViewById(R.id.bookTitle));
		setBodyText(position, (TextView) convertView.findViewById(R.id.bookText));

		return convertView;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id)
	{
		if(position<list.size() && getItem(position)!=null) {
			Intent intent = new Intent(context, BookDetailActivity.class);
			intent.putExtra(BookItem.BOOK_ITEM_STRING, getItem(position));
			intent.putExtra("enableDelete", false);
			((BookSearchActivity)context).startActivity(intent);
		}
	}
	
	@Override
	public void onClick(View v)
	{
		final int position  = (Integer) v.getTag();
		String shelfName = ((BookSearchActivity) context).getIntent().getStringExtra("SHELF_NAME");
		Intent intent = new Intent();
		intent.putExtra("SHELF_NAME", shelfName);
		intent.putExtra(BookItem.BOOK_ITEM_STRING, getItem(position));
		((BookSearchActivity) context).setResult(Activity.RESULT_OK, intent);
		((BookSearchActivity) context).finish();
	}
	
	// ////////////// private //////////////////////

	private void setImageFromUrl(int position, View convertView)
	{
		ProfileImageView imgView = (ProfileImageView) convertView.findViewById(R.id.bookProfileImg);
		imgView.setImageBitmap(list.get(position).getBitmap());
	}
	
	private void setTitle(int position, TextView titleView)
	{
		SpannableStringBuilder titleBuilder = new SpannableStringBuilder();
		titleBuilder.append(Html.fromHtml(list.get(position).getTitle()));
		titleView.setText(titleBuilder);
	}

	private void setBodyText(int position, TextView bodyView)
	{
		SpannableStringBuilder sb = new SpannableStringBuilder();
		sb.append(context.getString(R.string.publish_date));
		sb.append(" : ");
		sb.append(list.get(position).getPubdate());
		sb.append("\n");
		sb.append(Html.fromHtml(list.get(position).getDescription()));
		bodyView.setText(sb);
	}

}
